package com.xh.demo.config;

import com.xh.demo.common.consts.ConfigConst;
import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

import javax.servlet.Filter;

/**
* @Name WebAppInitializer
* @Description web容器启动时候创建对象（调用方法初始化容器以及前端控制器）
* @Author wen
* @Date 2019/7/31
* @return
*/
public class WebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

    /** 获取根容器的配置类（Spring的配置文件）父容器 **/
    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class[]{AppConfig.class};
    }

    /** 获取Web容器配置类（SpringMVC容器配置文件）子容器 **/
    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class[]{DispatcherServlet.class};
    }

    /** 获取DispatcherServlet的映射信息 **/
    @Override
    protected String[] getServletMappings() {
        return new String[]{"/"};
    }

    /**
     * 添加过滤器
     * @return
     */
    @Override
    protected Filter[] getServletFilters() {
        CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
        characterEncodingFilter.setEncoding(ConfigConst.DEFAULT_CHARSET_ENCODING_UTF8);
        characterEncodingFilter.setForceEncoding(true);
        return new Filter[]{characterEncodingFilter};
    }
}